package resturant;

import static com.wagnerandade.coollection.Coollection.*;

import java.util.Collection;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;

public class ResturantReport {
	
	private MenuCollection MenuCollection;
	private OrderCollection OrdersCollection;
	private TreeMap<String,Integer> OrderTracker =	new	TreeMap<String,Integer>();
	
	public ResturantReport(TreeMap<OrderItem, String> _Orders,TreeSet<MenuItem> _Menu) {

		MenuCollection = new MenuCollection(_Menu);
		OrdersCollection = new OrderCollection(_Orders);		
	}
	
	public void GenerateReport()
	{
		String report = "\n";
		report += GenerateMenuReport() + "\n";
		report += GenerateFrequencyReport() + "\n";
		report +=  GenerateExtraReport() + "\n";
		report +=  GenerateTableReport() + "\n";
		System.out.println(report);
	}
	
	private String GenerateMenuReport()
	{
		String report = "MENU \n==== \n" ;
		TreeMap<String,TreeSet<MenuItem>> CategoryTracker =	new	TreeMap<String,TreeSet<MenuItem>>();
		TreeSet<MenuItem> item;
		for (MenuItem o : MenuCollection) {
			String category = o.getCategory();
			if(CategoryTracker.containsKey(category)) {
				item = CategoryTracker.get(category);
			}
			else{
				item = new TreeSet<MenuItem>();
			}
			item.add(o);
			CategoryTracker.put(o.getCategory(), item);
			
		}
		
		for (Entry<String, TreeSet<MenuItem>> ckey : CategoryTracker.entrySet()) {
			report +=  ckey.getKey() + "  " + "\n";
			for(Object x :  ckey.getValue().toArray()){
				report +=  "  " + ((MenuItem) x).getName() + "  " + ((MenuItem) x).getPrice() + "\n";
			}
			report += "\n";
		}
		

		return report;
	} 
	
	private String GenerateFrequencyReport()
	{
		String report = "FREQUENCY REPORT \n================= \n" ;
		Integer count = 0; 
		 
		for (OrderItem o : OrdersCollection) {
			count = o.getQuantity();
			if(OrderTracker.containsKey(o.getOrderName())) {count = OrderTracker.get(o.getOrderName()) + count;}
			OrderTracker.put(o.getOrderName(), count);
			count = 0;
		}
		
		for (String key : OrderTracker.keySet()) {
			report +=  key + "  " + String.format("%-20s",OrderTracker.get(key).toString()) + "\n";
		}
		
			return report;
		
	}
	
	private String GenerateExtraReport()
	{
		String report = "DISHES NOT ORDERED \n================= \n" ;
		TreeSet<String> NotOrdered = new TreeSet<String>();  
		for (MenuItem o : MenuCollection) {
			String item = o.getName();
			if(!OrderTracker.containsKey(item)) {
			NotOrdered.add(item);
			}
		}
		
		for (String key : NotOrdered) {
			report +=  key + "  " + "\n";
		}
		
			return report;
		
	}

	private String GenerateTableReport()
	{
		String report = "TABLE SUMMARY \n============= \n" ;
		
		return report;
	}
}
